package net.hnjdzy.newsclient;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Logon extends AppCompatActivity{
    private EditText pwd,phone;
    private Button bon,bon2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.logon);
        bon = (Button)findViewById(R.id.bon);
        bon2=(Button)findViewById(R.id.bon2);
        bon2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(Logon.this,register.class);
                  startActivity(intent);
            }
        });

        bon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url="http://39.99.228.8:8080/news/servlet/ApiServlet?opr=register";
                 delv a= new delv();
                 a.execute(url);

            }
        });

    }

    class  delv extends AsyncTask<String,Void,bean>{

        protected void onPostExecute(bean s) {
           phone = (EditText) findViewById(R.id.phone);
           pwd =(EditText) findViewById(R.id.pwd);
           String ph = phone.getText().toString();
           int  pw = Integer.parseInt(pwd.getText().toString());
           if(s!=null) {
               if (phone == null || pwd == null || !ph.equals(s.getName()) ||pw!=s.getPwd()) {
                   Toast.makeText(Logon.this, "手机号或密码不正确", Toast.LENGTH_SHORT).show();
               }  else {
                   Intent integer = new Intent(Logon.this,GuideActivity.class);
                   startActivity(integer);
               }
           }
        }

        @Override
        protected bean doInBackground(String... strings) {
            Log.d("Test","哈哈");
            String url = strings[0];
            OkHttpClient client = new OkHttpClient();
            Request request =new Request.Builder().url(url).build();

            try {
                Response response =client.newCall(request).execute();
              String s = response.body().string();
                  bean  bean  = new bean();
                Log.d("Test","哈哈哈哈1"+s);
                JSONObject js= new JSONObject(s);

             bean.setName(js.getString("uname"));
              bean.setPwd(js.getInt("upwd"));

             /* bean.setName("123");
              bean.setPwd(123456);*/
                Log.d("Test",bean.getName()+bean.getPwd()+"哈哈哈哈");
              return  bean;
            } catch ( Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }

}
